package com.dotstone.chipism.adapter;

import java.util.List;

import com.dotstone.chipism.R;
import com.dotstone.chipism.bean.Device;
import com.dotstone.chipism.util.SocketManager;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class WlanHorizonAdapter extends BaseAdapter {

	private Context context;
	private List<Device> data;
	private HoldView holdView;

	public WlanHorizonAdapter(Context _context, List<Device> data) {
		this.data = data;
		this.context = _context;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		if (view == null) {
			view = LayoutInflater.from(context).inflate(R.layout.item_horizontal_wlan_list, null);
			holdView = new HoldView();
			holdView.deviceTv = (TextView) view.findViewById(R.id.device_name_tv);
			holdView.deviceImg = (ImageView) view.findViewById(R.id.device_img);
			view.setTag(holdView);
		} else {
			holdView = (HoldView) view.getTag();
		}
		Device d = data.get(position);
		holdView.deviceTv.setText(d.getDeviceName());
		if (SocketManager.getInstance().sMap.get(d.getDeviceID()) != null) {
			// Log.i("wmy",
			// "SocketManager.getInstance().isRemoteSuccess() = " +
			// SocketManager.getInstance().isRemoteSuccess()
			// + " " + d.getDeviceID() + " "
			// + SocketManager.getInstance().mStatusMap.get(d.getDeviceID()));
			if (SocketManager.getInstance().mStatusMap.get(d.getDeviceID()) != null) {
				if (SocketManager.getInstance().sMap.get(d.getDeviceID()).isLocalSuccess()
						|| ((SocketManager.getInstance().isRemoteSuccess())
								&& SocketManager.getInstance().mStatusMap.get(d.getDeviceID()) == 1)) {
					holdView.deviceImg.setImageResource(R.drawable.btw2);
					holdView.deviceTv.setTextColor(context.getResources().getColor(R.color.grayon));
				} else {
					holdView.deviceImg.setImageResource(R.drawable.btw1);
					holdView.deviceTv.setTextColor(context.getResources().getColor(R.color.grayoff));
				}
			}
		}
		return view;
	}

	class HoldView {
		private TextView deviceTv;
		private ImageView deviceImg;
	}

}
